package com.leantech.android.browser.ui.search.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.cscj.android.repository.db.entity.HotSearchEntity
import com.leantech.android.browser.databinding.ItemSearchHistoryBinding
import com.leantech.android.browser.utils.onThrottleClick

class SearchRecommendAdapter(private val adapterCallback: SearchRecommendItemAdapterCallback) :
    ListAdapter<HotSearchEntity, SearchRecommendAdapter.SearchRecommendItemViewHolder>(diffCallback) {

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<HotSearchEntity>() {
            override fun areItemsTheSame(
                oldItem: HotSearchEntity, newItem: HotSearchEntity
            ): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(
                oldItem: HotSearchEntity, newItem: HotSearchEntity
            ): Boolean {
                return oldItem == newItem
            }
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): SearchRecommendItemViewHolder {
        val binding =
            ItemSearchHistoryBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SearchRecommendItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: SearchRecommendItemViewHolder, position: Int) {
        holder.bind(getItem(position), adapterCallback)
    }

    interface SearchRecommendItemAdapterCallback {
        fun onSearchRecommendItemClicked(data: HotSearchEntity)
    }

    class SearchRecommendItemViewHolder(private val binding: ItemSearchHistoryBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(data: HotSearchEntity, adapterCallback: SearchRecommendItemAdapterCallback) {
            binding.title.text = data.content
            binding.root.onThrottleClick {
                adapterCallback.onSearchRecommendItemClicked(data)
            }
        }

    }


}